package com.kayt.cyberBar.cuser.business.dao.impl;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cuser.business.dao.IHandWorkDao;
import com.kayt.cyberBar.cuser.business.entity.po.BHandWork;

@Repository("handWorkDao")
public class HandWorkDaoImpl implements IHandWorkDao {

	@Autowired
	private EntityDao entityDao;

	@Override
	public void save(BHandWork entity) throws SQLException {
		entityDao.save(entity);
	}
	
	@Override
	public PageData<Map<String, Object>> getHandWorkDetailPage(Integer pageNum, Integer pageSize, String storeCode,
			String handWorkType,String handDate) throws SQLException {
		PageData<Map<String, Object>> resultPage = new PageData<>();
		String dataSql = "SELECT bw.HAND_USER_WORK_CODE,bw.NEXT_USER_WORK_CODE,bw.HAND_WORK_CODE,bw.HAND_SHIFT_CODE,"
				+ "wd.ONLINE_OTHER_SALES_COUNT,wd.OFFLINE_OTHER_SALES_COUNT,wd.ONLINE_INTERNET_FEE_SALES,wd.OFFLINE_INTERNET_FEE_SALES,wd.ONLINE_ITEM_FEE_SALES,wd.OFFLINE_ITEM_FEE_SALES,"
				+ "bw.HAND_DATE,bw.CREATE_TIME,wd.INTERNET_FEE_SALES,wd.OTHER_SALES_COUNT,wd.ITEM_FEE_SALES,wd.TOTAL_RETURNS,wd.SHIFT_DEFRAY,wd.SHIFT_SALES_COUNT,"
				+ "wd.HAND_DESCRIPTION,bw.STATUS_CODE , bw.INVENTORY_STATUS FROM b_user u, b_hand_work bw,b_hand_work_detail wd,"
				+ " b_organization_depart_user du, b_organization_store_depart sd, b_sheet_user_work uw WHERE u.USER_CODE = du.USER_CODE "
				+ (StringUtils.isEmpty(handWorkType) ? "" : "and bw.HAND_WORK_TYPE = '" + handWorkType + "'")
				+ (StringUtils.isEmpty(handDate) ? "" : "and bw.HAND_DATE = '" + handDate + "'")
				+ "AND du.STORE_DEPART_CODE = sd.STORE_DEPART_CODE and uw.USER_CODE = u.USER_CODE and uw.WORK_CODE = "
				+ "bw.HAND_USER_WORK_CODE AND bw.HAND_WORK_CODE = wd.HAND_WORK_CODE and sd.STORE_CODE = '" + storeCode
				+ "'  order by bw.CREATE_TIME desc";
		String countSql = "SELECT count(*) as count FROM b_user u, b_hand_work bw,b_hand_work_detail wd, b_organization_depart_user du,"
				+ " b_organization_store_depart sd, b_sheet_user_work uw WHERE u.USER_CODE = du.USER_CODE "
				+ (StringUtils.isEmpty(handWorkType) ? "" : " and bw.HAND_WORK_TYPE = '" + handWorkType + "'")
				+ (StringUtils.isEmpty(handDate) ? "" : "and bw.HAND_DATE = '" + handDate + "'")
				+ "AND du.STORE_DEPART_CODE = sd.STORE_DEPART_CODE and uw.USER_CODE = u.USER_CODE and uw.WORK_CODE = "
				+ "bw.HAND_USER_WORK_CODE AND bw.HAND_WORK_CODE = wd.HAND_WORK_CODE and sd.STORE_CODE = '" + storeCode
				+ "'";
		List<Map<String, Object>> dataMapList = entityDao.getPagingDataBySql(dataSql, pageNum, pageSize, null);
		List<Map<String, Object>> countMapList = entityDao.getPagingDataBySql(countSql, pageNum, pageSize, null);
		BigInteger count = (BigInteger) countMapList.get(0).get("count");
		resultPage.setPageList(dataMapList);
		resultPage.setTotal(count.longValue());
		return resultPage;
	}

	@Override
	public BHandWork getHandWorkByCode(String handWorkCode) throws SQLException {
		String sql = "select * from b_hand_work where hand_work_code = '" + handWorkCode + "'";
		List<BHandWork> handWorkList = entityDao.getPagingDataBySql(BHandWork.class, -1, -1, sql, null);
		if (handWorkList != null && handWorkList.size() != 0) {
			return handWorkList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void update(BHandWork entity) throws SQLException {
		entityDao.update(entity);
	}

	@Override
	public List<BHandWork> findAllByDate(String date) {
		try {
			return entityDao.getPagingDataBySql(BHandWork.class, -1, -1, "select * from b_hand_work o where o.HAND_DATE = '"+date+"'", null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<BHandWork> findBySql(Integer pageNum,Integer pageSize,String sql,Object[] queryParams) throws SQLException{
		return entityDao.getPagingDataBySql(BHandWork.class,pageNum, pageSize, sql, queryParams);
	}
}
